package com.lagou;

import com.lagou.bean.ConsumerComponet;

import com.lagou.filter.MyTPMonitorFilter;
import com.lagou.utils.ConsumerThread;
import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.Random;
import java.util.concurrent.*;

public class AnnotationConsumerMain {
    public static void main(String[] args) throws IOException, InterruptedException {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ConsumerConfiguration.class);
        context.start();
        //获取消费者组件
        ConsumerComponet consumer = context.getBean(ConsumerComponet.class);
        ExecutorService executorServiceA = Executors.newFixedThreadPool(10);
        ExecutorService executorServiceB = Executors.newFixedThreadPool(10);
        ExecutorService executorServiceC = Executors.newFixedThreadPool(10);

        //每5s输出一次性能检测数据
        //输出运行结果
        ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
        scheduledExecutorService.scheduleWithFixedDelay(new Runnable() {
            @Override
            public void run() {
                MyTPMonitorFilter.calcMethodTP("methodA");
                MyTPMonitorFilter.calcMethodTP("methodB");
                MyTPMonitorFilter.calcMethodTP("methodC");
            }
        }, 5, 5, TimeUnit.SECONDS);

        //一分钟之内线程并行调用
        //methodA B C
        executorServiceA.execute(new ConsumerThread("A",consumer));
        executorServiceB.execute(new ConsumerThread("B",consumer));
        executorServiceC.execute(new ConsumerThread("C",consumer));

        Thread.sleep(60*1000);
        System.out.println("====一分钟时间到====");

        //一分钟之后定时器任务结束
        System.out.println("====开始关闭定时器任务====");
        scheduledExecutorService.shutdown();
        boolean scheduledExecutorServiceStatus = scheduledExecutorService.awaitTermination(10, TimeUnit.SECONDS);
        if(scheduledExecutorServiceStatus){
            System.out.println("====定时器任务全部关闭====");
        }

      System.out.println("====开始关闭线程并行调用====");
        executorServiceA.shutdown();
        executorServiceB.shutdown();
        executorServiceC.shutdown();
      boolean executorServiceStatusA = executorServiceA.awaitTermination(60, TimeUnit.SECONDS);
//      while(!executorServiceStatusA){
//          //System.out.println("====正在关闭线程池A并行调用任务====");
//      }
//        if(executorServiceStatusA)
//            System.out.println("====线程池A并行调用任务已经全部关闭====");
        boolean executorServiceStatusB = executorServiceB.awaitTermination(60, TimeUnit.SECONDS);
//        while(!executorServiceStatusB){
//            //System.out.println("====正在关闭线程池B并行调用任务====");
//        }
//        if(executorServiceStatusB)
//            System.out.println("====线程池B并行调用任务已经全部关闭====");
        boolean executorServiceStatusC = executorServiceC.awaitTermination(60, TimeUnit.SECONDS);
//        while(!executorServiceStatusC){
//            //System.out.println("====正在关闭线程池C并行调用任务====");
//        }
//        if(executorServiceStatusC)
//            System.out.println("====线程池C并行调用任务已经全部关闭====");



//        while (true){
////            System.in.read();
////            String world = consumer.sayHello("world");
////            System.out.println("result:"+world);
////        }
    }

    @Configuration
    @PropertySource("classpath:/dubbo-consumer.properties")
    @ComponentScan(basePackages = "com.lagou.bean")
    @EnableDubbo
    static  class ConsumerConfiguration{

    }



}
